//  Inside Microsoft Exchange 2007 Web Services 
//  Chapter 10
//
//  Copyright (c) 2007 David Sterling, Ben Spain, Mark Taylor, Huw Upshall, Michael Mainer.  
//  All rights reserved.
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using ProxyHelpers.EWS;

namespace RemoveACancelledMeeting
{
    class Program
    {
        static void Main(string[] args)
        {
            ExchangeServiceBinding binding = new ExchangeServiceBinding();
            binding.Url = @"https://yourServer/ews/Exchange.asmx";
            binding.Credentials = new NetworkCredential(
                "username",
                "password",
                "domain");

            // TODO: You must have the ItemId of a cancelled meeting in order to call RemoveACancelledMeeting.
        }

        /// <summary>
        /// Removes a meeting from the calendar that has been cancelled.  Removes the 
        /// meeting cancellation as well. (Listing 10-35)
        /// </summary>
        /// <param name="binding">Binding to use for the web service call</param>
        /// <param name="meetingCancellationId">Id of the meeting 
        /// cancellation message</param> 
        public static void RemoveACancelledMeeting(
            ExchangeServiceBinding binding,
            ItemIdType meetingCancellationId)
        {
            // Create the RemoveItemType instance
            //
            RemoveItemType removeItem = new RemoveItemType();
            removeItem.ReferenceItemId = meetingCancellationId;

            // Assemble our CreateItem request
            //
            CreateItemType createItemRequest = new CreateItemType();
            createItemRequest.MessageDisposition = MessageDispositionType.SendOnly;
            createItemRequest.MessageDispositionSpecified = true;
            createItemRequest.Items = new NonEmptyArrayOfAllItemsType();
            createItemRequest.Items.Items =
                new RemoveItemType[] { removeItem };

            // Call the CreateItem web method and check for success.
            //
            CreateItemResponseType response = binding.CreateItem(createItemRequest);
            ItemInfoResponseMessageType responseMessage =
                response.ResponseMessages.Items[0] as ItemInfoResponseMessageType;
            if (responseMessage.ResponseCode != ResponseCodeType.NoError)
            {
                throw new Exception(String.Format(
                    "Unable to remove the meeting and/or " +
                    "meeting cancellation message\r\n{0}\r\n{1}",
                    responseMessage.ResponseCode,
                    responseMessage.MessageText));
            }
        }

    }
}
